<?php

namespace App\Models\Order;

use Illuminate\Database\Eloquent\Model;

class Saleimplementation extends Model
{
    protected $table = 'sale_implementation';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;
        // $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;
        /***
         * 子订单查询 也需要过滤，is_del 2 数值
         *
         *
        */
        $query = self::whereHas('suborder',function($q){
            $q->where('is_del','1');
        })->with(['suborder'=>function($q){
            $q->where('is_del','1');
        },'saleorder'])
        ->where('is_del', config('constants.is_del.no'))->orderBy('create_at', 'desc');

        //单据日期
        $start_time = $request->start_time;
        if ($start_time != '') {
            $query->where('document_date', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $query->where('document_date', '<=', $end_time);
        }

        //客户名称
        $custom_name = $request->custom_name;
        if ($custom_name != '') {
            $query->where('custom_name', 'like', '%' . $custom_name . '%');
        }

        //manger 1自己添加的数据，2所有数据
        $manger = $request->manger;
        if ($manger == 1 || $utype == '7') {
            $query->where('manger_id', '=', $manger_id);
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        // if ($sys_dpm_id > 0) {
        //     $query->where('sys_dpm_id', $sys_dpm_id);
        // }

        $status = $request->status ?? 0;
        if ($status > 0) {
            $query->where('status', $status);
        }

        $settle_status = $request->settle_status ?? 0;
        if ($settle_status > 0) {
            $query->where('settle_status', $settle_status);
        }
        /***
         * 是否是审核列表
         *
        */
        $is_audit = $request->is_audit??0;
        if($is_audit > 0){
            $query->where('is_audit', $is_audit);
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        }

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 详情
     */
    public static function getone($id)
    {
        return self::with(['suborder' => function ($query) {
            $query->where('is_del', config('constants.is_del.no'));
        }])->where('id', $id)
            ->where('is_del', config('constants.is_del.no'))
            ->get();
    }

    /**
     * 客户余额
     */
    public static function custombalance($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;

        $query = self::select('id', 'order_number', 'custom_id', 'custom_name', 'settlement_company', 'sale_amount', 'settle_status')
            ->where('is_del', config('constants.is_del.no'))
            ->orderBy('create_at', 'desc');

        //客户名称
        $custom_name = $request->custom_name;
        if ($custom_name != '') {
            $query->where('custom_name', 'like', '%' . $custom_name . '%');
        }

        //结算公司
        $settlement_company = $request->settlement_company;
        if ($settlement_company != '') {
            $query->where('settlement_company', 'like', '%' . $settlement_company . '%');
        }

        //订单完结状态 1未完结，2已完结
        $settle_status = $request->settle_status;
        if ($settle_status != '') {
            $query->where('settle_status', $settle_status);
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        /**
         * manger 1自己添加的数据，2所有数据
         * 销售只能看到自己数据
         *
        */
        $manger = $request->manger;
        if ($manger == 1 || $utype == '7') {
            $query->where('manger_id', '=', $manger_id);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        }

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 获取子订单
     */
    public function suborder()
    {
        return $this->hasMany(Saleimplementationsub::class, 'p_id', 'id');
    }

    public function saleorder()
    {
        return $this->hasOne(Saleorder::class, 'order_number', 'order_number');
    }
}
